<template>
  <div class="header">
    <div
      v-for="item in list"
      :key="item.name"
      :class="{ active: item.isActive }"
      @click="clickList(item)"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
import { reactive, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
export default {
  name: 'Header',
  setup() {
    const list = reactive([
      { name: '推荐', isActive: false, url: '/recommend' },
      { name: '歌手', isActive: false, url: '/singer' },
      { name: '榜单', isActive: false, url: '/rank' },
      { name: '搜索', isActive: false, url: '/search' }
    ])
    const router = useRouter()
    const route = useRoute()

    function clickList(item) {
      list.forEach(ele => {
        ele.isActive = false
      })
      item.isActive = true
      router.push(item.url)
    }

    watch(
      () => route.path,
      newParams => {
        const res = list.find(item => item.url === newParams)
        res && clickList(res)
      }
    )

    return {
      list,
      clickList
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  @include bg-color;
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 44px;
  color: $color-text-on-bg;
  font-size: $font-size-medium;
  > div {
    position: relative;
    font-weight: 100;
  }
  > .active {
    &:after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 0;
      width: 100%;
      height: 0;
      border-bottom: 1px solid #fff;
    }
  }
  .icon-wrapper {
    color: red;
  }
}
</style>
